﻿import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/Home.vue'
import HoFivehome from '../views/Fivehome.vue'
import { Toast } from 'vant'
Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    name: 'Home',
    component: HoFivehome,
    redirect: "/home",
    children: [
      {
        path: '/home',
        name: 'Home',
        component: Home,
        meta: {
          id: 0
        }
      },
      {
        path: '/special',
        name: 'Special',
        component: () => import('@/components/special/Special.vue'),
        meta: {
          id: 1
        }
      },
      {
        path: '/classify',
        name: 'Classify',
        component: () => import('@/components/classify/Classify.vue'),
        meta: {
          id: 2
        }
      },
      {
        path: '/shopping',
        name: 'Shopping',
        component: () => import('@/components/shopping/Shopping.vue'),
        meta: {
          id: 3,
          requireAuto: true
        },
      },

      {
        path: '/user',
        name: 'User',
        component: () => import('@/components/user/User.vue'),
        meta: {
          id: 4
        }
      }
    ],
  },
  {
    path: '/productdetail/:id',
    name: 'ProductDetail',
    component: () => import('@/components/detail/ProductDetail'),
  },
  {
    path: '/channel/:id',
    name: 'Channel',
    component: () => import('@/components/classify/Channel')
  },
  {
    path: '/listDetails/:id/:title',
    name: 'ListDetails',
    component: () => import('@/components/home/ListDetails'),
  },
  {
    path: '/firstListDetails/:id/:title',
    name: 'firstListDetails',
    component: () => import('@/components/detail/home/FirstListDetails'),
  },
  {
    path: '/user/address',
    name: 'AddressList',
    component: () => import('@/components/user/children/AddressList'),
    meta: {
      requireAuto: true
    }
  },
  {
    path: '/user/proposal',
    name: 'Proposal',
    component: () => import('@/components/user/children/Proposal'),
    meta: {
      requireAuto: true
    }
  },
  {
    path: '/user/online',
    name: 'OnLine',
    component: () => import('@/components/user/children/OnLine'),
    meta: {
      requireAuto: true
    }
  },
  {
    path: '/user/trace',
    name: 'trace',
    component: () => import('@/components/user/children/Trace'),
    meta: {
      requireAuto: true
    }
  },
  {
    path: "/shoppding/order",
    name: "order",
    component: () => import("@/components/shopping/order/Order.vue"),
    meta: {
      requireAuto: true
    }
  },
  {
    path: "/shoppding/orderinfo",
    name: "orderinfo",
    component: () => import("@/components/shopping/order/OrderInfo.vue"),
    meta: {
      requireAuto: true
    }
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})



//路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to); 123132

  const token = localStorage.getItem("token");
  // to目标路由
  // from原路由
  // next跳转到路由目标
  if (!token) {
    if (to.meta.requireAuto) {
      Toast.fail("请先登录");
      next("/user")
    } else {
      next();
    }
  } else {
    next();
  }


})
export default router
